﻿package component.tree.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import component.tree.sql.TreeSqlFactory;
import component.tree.entity.Tree;
import component.tree.model.TreeModel;
import component.tree.model.TreeModelFactory;
import component.tree.service.ITreeDataProvider;
import component.tree.service.ITreeService;
import component.tree.service.TreeService;
import component.tree.util.StringUtil;
import component.tree.util.TreeUtil;
import component.tree.web.view.TreeSelectView;
import component.tree.web.view.TreeView;

public class TreeAction {
	static ITreeService treeService = new TreeService();
	
	public List getRootTreeData(String componentId,String condition,HttpServletRequest request){
		List rootList = null;
		TreeModel treeModel = TreeModelFactory.getTreeModel(componentId);
		treeModel.setCondition(condition);
		String dataProvider = treeModel.getDataProvider();
		if(StringUtil.isNull(dataProvider)){
			String sql = TreeSqlFactory.getTreeSql(treeModel, true);
			ITreeService service = new TreeService();
			rootList = service.getDataBySql(sql, new Tree());
		}else{
			ITreeDataProvider treeService = (ITreeDataProvider)this.getObject(dataProvider);
			HttpSession session = request.getSession();
			rootList = treeService.getTreeData(null,SessionHelper.getConditionMap(request,session, componentId));
		}
		return rootList;
	}
	
	public List getChildListByParentId(String parentId,String componentId,String condition,HttpServletRequest request){
		List childList = null;
		TreeModel treeModel = TreeModelFactory.getTreeModel(componentId);
		treeModel.setCondition(condition);
		String dataProvider = treeModel.getDataProvider();
		if(StringUtil.isNull(dataProvider)){
			String sql = TreeSqlFactory.getChildSql(parentId, treeModel);
			ITreeService service = new TreeService();
			childList = service.getDataBySql(sql, new Tree());
		}else{
			ITreeDataProvider treeService = (ITreeDataProvider)this.getObject(dataProvider);
			HttpSession session = request.getSession();
			childList = treeService.getTreeData(null,SessionHelper.getConditionMap(request,session, componentId));
		}
		return childList;
	}
	
	public List getAllTreeData(String componentId,String condition,String searchName,boolean isSearchAble,HttpServletRequest request){
		List treeList = null;
		TreeModel treeModel = TreeModelFactory.getTreeModel(componentId);
		
		//设置页面设置的条件和用户输入的条件
		if (!StringUtil.isNull(searchName) || isSearchAble) {
			condition += " and " + treeModel.getFnameName() + " like '%" + searchName.trim() + "%' ";
		}
		treeModel.setCondition(condition);
		
		String dataProvider = treeModel.getDataProvider();
		if(StringUtil.isNull(dataProvider)){
			String sql = TreeSqlFactory.getTreeSql(treeModel, false);
			ITreeService service = new TreeService();
			treeList = service.getTreeDataBySql(sql, new Tree());
		}else{
			ITreeDataProvider treeService = (ITreeDataProvider)this.getObject(dataProvider);
			HttpSession session = request.getSession();
			treeList = treeService.getTreeData(null,SessionHelper.getConditionMap(request,session, componentId));
		}
		return treeList;
	}
	
	private boolean getBoolean(String booleanStr) {
		if (StringUtil.isNull(booleanStr)) {
			return false;
		} else {
			return (new Boolean(booleanStr)).booleanValue();
		}
	}
	
	private boolean getBoolean2(String booleanStr) {
		if (StringUtil.isNull(booleanStr)) {
			return true;
		} else {
			return (new Boolean(booleanStr)).booleanValue();
		}
	}

	public String toTreeSelect(ServletHelper helper) {
		return this.service(helper,new TreeSelectView());
	}

	public String doSearch(ServletHelper helper) {
		return this.service(helper,new TreeView());
	}
	
	public String service(ServletHelper helper,TreeView treeView) {
		HttpServletRequest request = helper.getRequest();
		setTreeTemplate(request, treeView);
		
		String componentId = treeView.getComponentId();
		String condition = treeView.getCondition();
		String searchName = treeView.getSearchName();
		boolean isSearchAble = treeView.isSearchAble();
		
		List treeList = null;
		boolean isLoadAll = treeView.isLoadAll();
		if(isLoadAll){
			treeList = this.getAllTreeData(componentId, condition, searchName, isSearchAble, request);
		}else{
			treeList = this.getRootTreeData(componentId, condition, request);
		}
		treeView.setTreeList(treeList);
		
		String tree = treeView.toString();
		helper.print(null, null, tree);
		return "";
	}

	public String doLoadChildNode(ServletHelper helper) {
		HttpServletRequest request = helper.getRequest();
		String divValue = request.getParameter("divValue");
		Tree parentVO = new Tree();
		parentVO = TreeUtil.getTree(parentVO,divValue);
		
		String componentId = request.getParameter("componentId");
		String condition = request.getParameter("condition");
		
		List treeList = this.getChildListByParentId(parentVO.getId(), componentId, condition, request);
		parentVO.setChildList(treeList);
		
		TreeView treeView = new TreeView("treeObj", request.getContextPath(), treeList);
		setTreeTemplate(request, treeView);

		String str = treeView.getTreeNodeHtml(parentVO);
		helper.write(helper.getResponse(), str);
		return "";
	}

	private void setTreeTemplate(HttpServletRequest request,
			TreeView treeView) {
		treeView.setContext(request.getContextPath());
		String title = request.getParameter("title");
		treeView.setTitle(title);
		String id = request.getParameter("id");
		treeView.setTreeObj(id);
		String componentId = request.getParameter("componentId");
		treeView.setComponentId(componentId);
		String condition = request.getParameter("condition");
		treeView.setCondition(condition);
		String useBaseIconAble = request.getParameter("isUseBaseIcon");
		treeView.setUseBaseIcon(getBoolean(useBaseIconAble));
		String isLoadAll = request.getParameter("isLoadAll");
		treeView.setLoadAll(getBoolean(isLoadAll));
		String firstShowLevel = request.getParameter("firstShowLevel");
		if (!StringUtil.isNull(firstShowLevel)) {
			treeView.setFirstShowLevel(Integer.parseInt(firstShowLevel));
		}
		String isSelectMore = request.getParameter("isSelectMore");
		treeView.setSelectMore(getBoolean(isSelectMore));
		String isUseLines = request.getParameter("isUseLines");
		treeView.setUseLines(getBoolean(isUseLines));
		String isUseIcons = request.getParameter("isUseIcons");
		treeView.setUseIcons(getBoolean(isUseIcons));
		String isSearch = request.getParameter("isSearch");
		treeView.setSearch(getBoolean2(isSearch));
		String isSearchAble = request.getParameter("isSearchAble");
		treeView.setSearchAble(getBoolean2(isSearchAble));
		String searchName = request.getParameter("searchName");
		if (searchName == null) {
			searchName = "";
		}
		treeView.setSearchName(searchName);
		String isRefresh = request.getParameter("isRefresh");
		treeView.setRefresh(getBoolean(isRefresh));
		String selectIds = request.getParameter("selectIds");
		treeView.setSelectIds(selectIds);
		
		String isShowClearBtn = request.getParameter("isShowClearBtn");
		treeView.setShowClearBtn(getBoolean2(isShowClearBtn));
		
		String isShowFirstBtn = request.getParameter("isShowFirstBtn");
		treeView.setShowFirstBtn(getBoolean2(isShowFirstBtn));
	}

	public void destroy() {

	}
	
	public Object getObject(String className){
		Class c = null;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Object obj = null;
		try {
			obj = c.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	
	
	/**
	 * 用于左边树右边列表
	 * 到右边列表页面
	 * @param helper
	 * @return
	 */
	public String toChildList(ServletHelper helper) {
		HttpServletRequest request = helper.getRequest();
		
		String parentId = request.getParameter("parentId");
		Tree parentVO = new Tree();
		parentVO.setId(parentId);
		
		String componentId = request.getParameter("componentId");
		String condition = request.getParameter("condition");
		
		List childList = this.getChildListByParentId(parentVO.getId(), componentId, condition, request);
		
		request.setAttribute("childList", childList);
		
		TreeModel treeModel = TreeModelFactory.getTreeModel(componentId);
		return treeModel.getListPage();
	}
}
